package dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DBController;
import model.Room;
import model.RoomType;
import dao.intf.RoomTypeDao;

public class RoomTypeDaoImpl implements RoomTypeDao {

	public List<RoomType> getRoomTypeList() {
		List<RoomType> roomTypeList = new ArrayList<RoomType>();
		
		ResultSet rs = DBController.retrieve("RoomType", null, null);
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					RoomType roomType = new RoomType();
					roomType.setId(rs.getString("ID"));
					roomType.setName(rs.getString("Name"));
					roomType.setDescription(rs.getString("Description"));
					roomType.setNoOfRooms(rs.getInt("No_Of_Rooms"));
					roomType.setRate(rs.getDouble("Rate"));
					
					roomTypeList.add(roomType);
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getRoomTypeList() of RoomTypeDaoImpl.");
			e.printStackTrace();
		}
		
		return roomTypeList;
	}

	public RoomType getRoomTypeById(String id) {
		RoomType roomType = new RoomType();
		
		ResultSet rs = DBController.retrieve("RoomType", null, "ID='"+id+"'");
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					roomType.setId(rs.getString("ID"));
					roomType.setName(rs.getString("Name"));
					roomType.setDescription(rs.getString("Description"));
					roomType.setNoOfRooms(rs.getInt("No_Of_Rooms"));
					roomType.setRate(rs.getDouble("Rate"));
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getRoomTypeById() of RoomTypeDaoImpl.");
			e.printStackTrace();
		}
		
		return roomType;
	}

	public RoomType getRoomDiscount(String id,int duration) {
		RoomType roomType = new RoomType();
		String queryStr="";
			   queryStr+=" SELECT  * FROM ROOMTYPE ";
			   queryStr+=" LEFT OUTER JOIN RATES ON ROOMTYPE.id=RATES.typeid ";
			   queryStr+=" AND RATES.typeid='"+id.trim()+"'";
			   queryStr+=" AND minnight <="+duration;
			   queryStr+=" AND (maxnight >="+duration+" OR maxnight=null)";
			   queryStr+=" WHERE ROOMTYPE.id='"+id.trim()+"'";
		ResultSet rs = DBController.retrieve(queryStr);
		
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					roomType.setId(rs.getString("ID"));
					roomType.setName(rs.getString("Name"));
					roomType.setDescription(rs.getString("Description"));
					roomType.setNoOfRooms(rs.getInt("No_Of_Rooms"));
					roomType.setRate(rs.getDouble("Rate"));
					roomType.setDiscountPercent(rs.getInt("DiscountPercent"));
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getRoomTypeById() of RoomTypeDaoImpl.");
			e.printStackTrace();
		}
		
		return roomType;
	}
	public int updateNoofRooms(String typeID,int noOfRooms) {
		int recordCount=0;
		try {
			
				String queryStr	 = " UPDATE RoomType ";
						queryStr+= " SET No_Of_Rooms="+noOfRooms+"";
						queryStr+= " WHERE ID='"+typeID.trim()+"'";
						
				recordCount=DBController.save(queryStr);
				
			
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at saveBooking BookingDaoImpl.");
			e.printStackTrace();
		}
		return recordCount;
	}

}
